package edu.ucsc.gameAI;

import java.awt.Color;

import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Constants.MOVE;
import pacman.game.Game;
import pacman.game.GameView;
//import edu.ucsc.gameAI.Debug.MESSAGETYPE;
import edu.ucsc.gameAI.decisionTrees.binary.IBinaryNode;
import edu.ucsc.gameAI.fsm.IState;

public class MoveTowardsClosestPowerPillAction implements IAction, IBinaryNode {

	GHOST closestGhost;
	double safeDistance = PacmanConstants.safeDistance;

	//int euclidianSafeDistance = 15;


	public void doAction() {
		/*
		 * Debug.debug("MoveTowardsClosestActivePowerPill", MESSAGETYPE.ACTION);
		 * 
		 * int pacManCurrentNode = game.getPacmanCurrentNodeIndex(); int[]
		 * activePowerPills = game.getActivePowerPillsIndices(); int
		 * closestPowerPillIndex =
		 * game.getClosestNodeIndexFromNodeIndex(pacManCurrentNode,
		 * activePowerPills, DM.PATH);
		 * 
		 * return game.getNextMoveTowardsTarget(pacManCurrentNode,
		 * closestPowerPillIndex, DM.PATH);
		 */
	}

	public MOVE getMove() {
		// TODO Auto-generated method stub
		return MOVE.RIGHT;
	}

	public MOVE getMove(Game game, IState state) {
		 //Debug.debug("MoveTowardsClosestActivePowerPill", Debug.MESSAGETYPE.ACTION);
		//System.out.println("Inside of MoveTowardsClosestPowerPill.java!");

		int pacmanIndex = game.getPacmanCurrentNodeIndex();
		// default to the first active power pill index

		int moveToIndex = -1;
		int closestPowerPillDistance = Integer.MAX_VALUE;
		// Loop through all available power pills, to find the next one to
		// eat.
		// The next one to eat should ideally NOT make Pacman cross through
		// a ghost
		

		
		boolean foundSafePill;
		for (int powerPillIndex : game.getActivePowerPillsIndices()) 
		{

			foundSafePill = true;
			//check to see if this power pill is farther away than another one we've found
			if (closestPowerPillDistance < game.getShortestPathDistance(pacmanIndex, powerPillIndex))
				continue;
			
			// powerPillIndex represents a power pill. Let's grab the shortest path to it.
			int[] pathToThisPillIndex = game.getShortestPath(pacmanIndex, powerPillIndex);

			// Great, now we have the shortest path, let's make sure that it
			// doesn't include a Ghost.
			for (GHOST ghostType : GHOST.values()) 
			{
				if(game.getGhostLairTime(ghostType) > 0 || game.isGhostEdible(ghostType))
				{
					continue;
				}

				int ghostIndex = game.getGhostCurrentNodeIndex(ghostType);

				//does the path include a ghost to it?
				for (int i = 0; i < pathToThisPillIndex.length; i++) {
					if (pathToThisPillIndex[i] == ghostIndex) {
						if (game.getShortestPathDistance(pacmanIndex, ghostIndex) < safeDistance) {
							// Ghost in the way!
							Debug.debug("The path to the pill index " + powerPillIndex + " has a dangerous ghost on it (" + ghostType.name() + ")", Debug.MESSAGETYPE.STATEMENT);
							foundSafePill = false;
							break;
						}
					}
				}
				
				//Ok, sure, maybe there isn't a ghost ON the path
				//but there could still be a ghost in the area.
				//We're gonna check to see the distance the ghost has to travel to the pill.
				int ghostDistanceFromPill = game.getShortestPathDistance(ghostIndex, powerPillIndex);
				int pacManDistanceFromPill = game.getShortestPathDistance(pacmanIndex, powerPillIndex);
				if(ghostDistanceFromPill < pacManDistanceFromPill){
					//the ghost is closer to the pill than pacman.
					//oh oh. Need to see if the path the ghost will take to get to the pill crosses the path that pacman will take to the pill.
					int numOverlap = 0;
					int[] ghostPathToPill = game.getShortestPath(ghostIndex, powerPillIndex);
					for(int i = 0; i < ghostPathToPill.length; i++){
						for(int j = 0; j < pathToThisPillIndex.length; j++ ){
							if(ghostPathToPill[i] == pathToThisPillIndex[j]){
								numOverlap++;
							}
							if(numOverlap >= 2){
								break;
							}
						}
						if(numOverlap >= 2){
							break;
						}
					}
					
					if(numOverlap >=2){
						//the ghost has to take the same freaking path (at least a little!)
						//Don't go down here!
						Debug.debug("YIKES! GHOSTS PATH TO PILL OVERLAPS WITH PACMAN!", Debug.MESSAGETYPE.STATEMENT);
						foundSafePill = false;
					}
					
				}
				
				
				
				if (!foundSafePill)
					break;
				
			}
			if (foundSafePill) {
				closestPowerPillDistance = game.getShortestPathDistance(pacmanIndex, powerPillIndex);
				moveToIndex = powerPillIndex;
			}
		}		
		
		if(moveToIndex != -1)
		{
			int[] powerPillPath = game.getShortestPath(pacmanIndex, moveToIndex);
			GameView.addPoints(game, Color.BLUE, powerPillPath );
			GameView.addPoints(game, Color.RED, moveToIndex);
			return game.getNextMoveTowardsTarget(pacmanIndex, moveToIndex, DM.PATH);
		}
		//no power pills are available, so just book it!
		else{
			Debug.debug("-----> ACTUALLY going to RunAwayFromGhosts", Debug.MESSAGETYPE.STATEMENT);
			return new RunAwayFromGhosts().getMove(game, state);
		}
	}

	public IAction makeDecision(Game game) {
		return this;
	}
	
	@Override
	public String toString(){
		return "MoveTowardsClosestPowerPillAction";
	}
}
